package com.jftone.bh.app.action;

import com.alibaba.fastjson.JSONObject;
import com.baidu.ueditor.ActionEnter;
import com.jftone.bh.app.service.OSSFileManagerService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.config.Const;
import org.jftone.util.DataMap;
import org.jftone.util.DateUtil;
import org.jftone.util.IData;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller(mapping="/ueditorAction")
public class UEditorAction extends BaseAction {

    @Autowired
    OSSFileManagerService ossFileService;

    public void config() {
        HttpServletRequest request = getRequest();
        HttpServletResponse response = getResponse();
        response.setContentType("application/json");
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        try {
            String exec="";
            String action=request.getParameter("action");
            if(action.equals("uploadimage")){
                String uuid= UUID.randomUUID().toString().replace("-","");
                DiskFileItemFactory factory= new DiskFileItemFactory();
                factory.setSizeThreshold(10*1024);	  //默认10K
                ServletFileUpload upload = new ServletFileUpload(factory);
                upload.setHeaderEncoding(Const.CHARSET_UTF8);
                upload.setFileSizeMax(1024*1024*50);		//最大文件大小

                IData<String, String> data = new DataMap<String, String>();
                FileItem fileItem = null;
                List<FileItem> listItem = upload.parseRequest(request);
                for (FileItem item : listItem) {
                    if(item.isFormField()){
                        data.put(item.getFieldName(), item.getString(Const.CHARSET_UTF8));
                    }else{
                        fileItem = item;
                    }
                }
                String pathSuffix = DateUtil.getNowStr("yyMM");
                String name = fileItem.getName();
                String fileExtension = name.substring(name.lastIndexOf(".")+1).toLowerCase();
                String key=pathSuffix+"/"+uuid+"."+fileExtension;
                String url=ossFileService.upload(fileItem.getInputStream(),"rich_text",key);

                Map<String,Object> map=new HashMap<>();
                map.put("state","SUCCESS");
                map.put("original",key);
                map.put("size",fileItem.getSize());
                map.put("title",key);
                map.put("type",fileExtension);
                map.put("url",url);
                exec= JSONObject.toJSONString(map);
            }else{
                exec = new ActionEnter(request, rootPath).exec();
            }
            PrintWriter writer = response.getWriter();
            writer.write(exec);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


